package com.goofengcn.website.application.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class BuildBaseData {
//
//	/**
//	 * @throws IOException
//	 */
//	public static Location BuildLocation(String file) throws IOException {
//		String line; // 用来保存每行读取的内容
//		InputStream is = new FileInputStream(file);
//		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//		line = reader.readLine(); // 读取第一行
//		line = reader.readLine(); // 读取第二行
//
//		Location zh_location = new Location();
//		zh_location.setCode("0086");
//		zh_location.setAirport(false);
//		zh_location.setCity(false);
//		zh_location.setCountry(true);
//		zh_location.setLandway(false);
//		zh_location.setName("中国");
//		zh_location.setOceanport(false);
//
//		/**
//		 * 读取location.csv文件，建立location数据 以电话区号作为code 以地区作为name 如果该电话号码已存在，则跳过
//		 * 所有的城市的都是zh_location的Children的成员
//		 */
//		while (line != null) { // 如果 line 为空说明读完了
//			Location location = new Location();
//
//			String[] arg = line.split(",");
//			String extcode = "0" + arg[0];
//			String dic = arg[2];
//			// String zip = arg[3];
//			location.setCode(extcode);
//			location.setName(dic);
//			boolean flag = true;
//
//			for (Location children : zh_location.getChildren()) {
//				if (extcode.equals(children.getCode()))
//					flag = false;
//			}
//
//			if (flag) {
//				zh_location.getChildren().add(location);
//				location.setParent(zh_location);
//			}
//
//			line = reader.readLine(); // 读取下一行
//		}
//
//		return zh_location;
//
//		// create
//
//		// WebApplicationContext wac =
//		// ContextLoader.getCurrentWebApplicationContext();
//	}
//
//	/**
//	 * @param locations
//	 * @return
//	 * @throws BadHanyuPinyinOutputFormatCombination
//	 */
//	public static String BuildLocationJson(Set<Location> locations)
//			throws BadHanyuPinyinOutputFormatCombination {
//		// ["上海", "sh", "shanghai", "021"]
//		StringBuilder sb = new StringBuilder(10000);
//		sb.append("var location=[");
//		boolean addDelimiter = false;
//
//		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 
//		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//  
//		format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);// 
//
//		for (Location location : locations) {
//			if (addDelimiter)
//				sb.append(", \n");
//			String py = "";
//			String pyFirst = "";
//			char[] chars = location.getName().toCharArray();
//			for (int i = 0; i < chars.length; i++) {
//				String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(
//						chars[i], format);
//				if (pinYin != null) {
//					py += pinYin[0];
//					pyFirst += pinYin[0].toCharArray()[0];
//				}
//			}
//			sb.append(String.format("[\"%1$s\", \"%2$s\", \"%3$s\", \"%4$s\"]",
//					location.getName(), pyFirst, py, location.getCode()));
//
//			addDelimiter = true;
//		}
//		sb.append("]");
//
//		return sb.toString();
//	}
//
}
